<?php
function createDateRangeArray($strDateFrom,$strDateTo) {
  $aryRange=array();

  $iDateFrom=mktime(1,0,0, substr($strDateFrom,5,2), substr($strDateFrom,8,2), substr($strDateFrom,0,4));
  $iDateTo=mktime(1,0,0, substr($strDateTo,5,2), substr($strDateTo,8,2), substr($strDateTo,0,4));

  if ($iDateTo>=$iDateFrom) {
    array_push($aryRange,date('Y-m-d',$iDateFrom));

    while ($iDateFrom<$iDateTo) {
      $iDateFrom+=86400; // add 24 hours
      array_push($aryRange,date('Y-m-d',$iDateFrom));
    }
  }
  return $aryRange;
}
//Rangos de fecha a migrar
$aryDates = createDateRangeArray('2011-07-15', '2011-08-03');
$iCnx = mysql_connect('localhost', 'root', 'passwd');
mysql_select_db('perucom2db', $iCnx);
//start date 2011-07-15
foreach ($aryDates as $strDate) {
  echo "Copiando visitas de la fecha: $strDate\n";
  mysql_query("REPLACE INTO visits_day (nid, day, total)
  SELECT nid, insert_date, COUNT(*)
  FROM visits
  GROUP BY nid, insert_date
  HAVING insert_date = '$strDate'", $iCnx);
  echo "esperando...\n";
  sleep(3);//dar respiro al server 3 segundos
}
mysql_close($iCnx);
?>
